<?php defined('SYSPATH') OR die('No direct access allowed.');

function smarty_function_box($params,&$smarty) {
    if (empty($params['name'])) { $smarty->trigger_error("assign: missing 'class' parameter"); return;  }
    $index = $params['name'];
    $id = '';
    if (isset($params['customid'])) {
        $id = str_replace('/','-',$params['customid']);
    } else {
        $id = str_replace('/','-',$index);
    }
    $index = explode('/',$index,2);
    $controllerName = ucfirst($index[0])."_Controller";
    $methodName = $index[1];
    $box = new $controllerName();
    $args = $params;
    unset($args['name']);
    unset($args['customid']);
    $result = call_user_func_array(array($box, 'box_'.$methodName), $args);
    if ($result instanceof Result) {
        $tmp = $this->_tpl_vars;
        $this->_tpl_vars = array();
        $this->assign($params);
        $this->assign($result->getArgs());
        $this->assign('this', $result);

        // a mindenkor hasznalt boxid-t beletesszuk
        // tegyuk az $id-hez kapasbol a '-box'-ot vagy ne? tisztabb kodot eredmenyezhetne
        $this->assign('controllerName',$index[0]);
        $this->assign('boxId',$id.'-box');

        $content = $this->fetch(kohana::find_file('views',$result->getTemplate(true),true,'tpl'));
        $this->_tpl_vars = $tmp;
    } else {
        $content = $result;
    }
    return '<div id="'.$id.'-box">'.$content.'</div>';
}

?>